<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 权限管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="addRight"
                >添加权限</el-button>
                <el-select v-model="query.key" placeholder="查询条件" class="handle-select mr10" @change="changeKey">
                    <el-option key="1" label="权限名" value="rightName"></el-option>
                    <el-option key="2" label="url" value="url"></el-option>
                    <el-option key="3" label="状态" value="status"></el-option>
                </el-select>
                <el-input v-model="query.value" placeholder="内容" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
<!--                <el-table-column type="selection" width="55" align="center"></el-table-column>-->
                <el-table-column prop="id" label="ID" width="135" align="center"></el-table-column>
                <el-table-column prop="rightName" label="名称"></el-table-column>
                <el-table-column prop="url" label="URL"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column prop="gmtModified" label="最后修改时间"></el-table-column>
                <el-table-column prop="status" label="状态">
                    <template #default="scope">
                        <el-switch active-color="#ff4949"
                                   v-model="scope.row.status"
                                   :active-value="1"
                                   :inactive-value="0" @change="prohibit(scope.row.id, scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                            <el-button
                                    type="text"
                                    icon="el-icon-edit"
                                    @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="详情" v-model="editVisible" width="30%">
            <el-form ref="form" :model="right" label-width="70px">
                <el-form-item label="权限名">
                    <el-input v-model="right.rightName"></el-input>
                </el-form-item>
                <el-form-item label="URL">
                    <el-input v-model="right.url"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="right.description"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import api from "../../../api";
    export default {
        name: "permission",
        data() {
            return {
                query: {
                    right:{
                        rightName: null,
                        url: null,
                        status:null

                    },
                    pageIndex: 1,
                    pageSize: 8,
                    key:"",
                    value:""
                },
                right:{
                    rightName: null,
                    url: null,
                    description:null
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                PermissionId: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            changeKey(val){
                this.query.key=val;
            },
            prohibit(id, row) {
                const status = row.status;
                api.prohibit(id, status,2).then(response => {
                    let msg = ""
                    if (status=='0') {
                        msg = "激活成功"
                    } else {
                        msg = "封禁成功"
                    }
                    this.$message({
                        type: 'success',
                        message: msg,
                    })
                    return response;
                }).catch(response => {
                    console.log(response.message)
                    this.$message({
                        type: 'error',
                        message: "操作失败",
                    })
                })
            },
            getData() {
                api.getRight(this.query.pageIndex,this.query.pageSize,this.query.right).then(res => {
                    this.tableData = res.data.item.records;
                    this.pageTotal = res.data.item.total;
                });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 触发搜索按钮
            handleSearch() {
                this.query.right[this.query.key]=this.query.value;
                this.query.pageIndex=1;
                this.getData();
                this.query.right[this.query.key]=null;
            },
            // 删除操作
            handleDelete(index,row) {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                })
                    .then(() => {
                        let array = [];
                        array[0]={"id":row.id};
                        api.deletePermission(array).then((resp) => {
                            if(resp.success){
                                this.$message.success("删除成功");
                                this.tableData.splice(index, 1);
                            }else {
                                this.$message.error("删除失败");
                            }
                        });

                    })
                    .catch(() => {});

            },
            // 编辑操作
            addRight() {
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                api.addRight(this.right).then(resp=>{
                    if(resp.success) {
                        this.$message.success(`添加成功`);
                        this.getData();
                    }else {
                        this.$message.error(`添加失败`);
                    }
                });
                this.editVisible = false;
            },
            // 分页导航
            handlePageChange(val) {
                this.query.pageIndex=val;
                this.getData(this.query.pageIndex,this.query.pageSize,this.query.right);
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
